import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

public class HashMapTest {
    public static void main(String[] args) {
        HashMap<Integer,String> hashMap = new HashMap<>();
        hashMap.put(1,"a");
        hashMap.put(1,"b");
        hashMap.put(2,"c");
        hashMap.put(4,"d");
        hashMap.put(3,"e");

        // 检查是否为空
        System.out.println(hashMap.isEmpty());

        // 输出map大小 插入五个元素，hashMap 自动去重，不允许有重复值，且会覆盖前一个值
        System.out.println(hashMap.size());
        // 查找指定键值是否存在
        if (hashMap.containsKey(10)){
            System.out.println("key:2"+"        value:"+hashMap.get(2));
        }else {
            System.out.println("this key not find");
            System.out.println("--------------------");
            System.out.println(hashMap.getOrDefault(10,"g"));
        }


        printHashMap01(hashMap);
        System.out.println("-----------------------------");
        printHashMap02(hashMap);

    }

    private static void printHashMap02(HashMap<Integer, String> hashMap) {
        for (Map.Entry<Integer,String> map : hashMap.entrySet()){
            System.out.print(map.getKey() + ":" + map.getValue() +" ");
        }
    }

    private static void printHashMap01(HashMap<Integer, String> hashMap) {
        Iterator<Integer> iterator = hashMap.keySet().iterator();
        while (iterator.hasNext()){
            System.out.print(hashMap.get(iterator.next())+" ");
        }


    }
}
